﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LearnDrawing.Controls
{
    /// <summary>
    /// WithStreamGeometry.xaml 的交互逻辑
    /// </summary>
    public partial class WithStreamGeometry : UserControl
    {
        public WithStreamGeometry()
        {
            InitializeComponent();
        }


        public string DrawingTime
        {
            get { return (string)GetValue(DrawingTimeProperty); }
            set { SetValue(DrawingTimeProperty, value); }
        }

        public static readonly DependencyProperty DrawingTimeProperty =
            DependencyProperty.Register("DrawingTime", typeof(string), typeof(WithStreamGeometry), new UIPropertyMetadata("", null));



        public WaveData WaveData
        {
            get { return (WaveData)GetValue(WaveDataDataProperty); }
            set { SetValue(WaveDataDataProperty, value); }
        }

        public static readonly DependencyProperty WaveDataDataProperty =
            DependencyProperty.Register("WaveData", typeof(WaveData), typeof(WithStreamGeometry), new UIPropertyMetadata(null, WaveDataPropertyChanged));

        private static void WaveDataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs arg)
        {
            WithStreamGeometry? control = obj as WithStreamGeometry;
            WaveData Data = (WaveData)arg.NewValue;

            if (control != null && Data != null)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                control?.DrawWaveImage(Data);
                stopwatch.Stop();
                control.DrawingTime = $"Using:{stopwatch.ElapsedMilliseconds}ms(WithStreamGeometry)";
            }
        }

        private void DrawWaveImage(WaveData Data)
        {
            StreamGeometry geometry = new()
            {
                FillRule = FillRule.EvenOdd
            };

            using (StreamGeometryContext ctx = geometry.Open())
            {
                ctx.BeginFigure(new Point(0, 0), true, false);
                for (int i = 0; i < 1900; i++)
                {
                    int x1 = i;
                    int y1 = Data.Datas[i];
                    ctx.LineTo(new Point(x1, y1), true, false);                    
                }
                ctx.LineTo(new Point(2000, 200), true, false);
            }
            geometry.Freeze();   
         

            GeometryDrawing aGeometryDrawing = new GeometryDrawing
            {
                Geometry = geometry,
                Brush = Brushes.Transparent,
                Pen = new Pen(Brushes.LightGreen, 1),
            };

            DrawingImage geometryImage = new DrawingImage(aGeometryDrawing);
            geometryImage.Freeze();

            var drawingImage = new Image { Source = geometryImage };
            drawingImage.Arrange(new Rect(0, 0, 2000, 400));
            drawingImage.Stretch = Stretch.None;

            RenderTargetBitmap? bitmap = new RenderTargetBitmap(2000, 400, 96, 96, PixelFormats.Pbgra32);
            bitmap.Render(drawingImage);

            this.imageWave.Source = bitmap;
           
        }
    }
}
